package tq.com.tqteacher.discuss.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.discuss.adapter.TQManagerMsgAdapter;
import tq.com.tqteacher.model.TQCommentOverInfoModel;
import tq.com.tqteacher.net.TQNetInterface;

public class TQDiscussMngActivity extends BaseTitleActivity {
    private TQDiscussMngFragment mDiscussMngFragment = new TQDiscussMngFragment();

    static public void show(int mId, int attId, Activity pActivity){
        Intent lIntent = new Intent(pActivity, TQDiscussMngActivity.class);
        lIntent.putExtra("AttachId", attId);
        lIntent.putExtra("meterialId", mId);
        pActivity.startActivity(lIntent);
    }
    @Override
    protected BaseFragment fragment() {
        return mDiscussMngFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("整理留言");
    }

    static public class TQDiscussMngFragment extends BaseFragment{
        private RecyclerView mRecyclerView;
        private TQManagerMsgAdapter mManagerMsgAdapter;
        private TextView mButton;

        private int attId = 0;
        private int meterialId = 0;

        private int page = 0;
        private SmartRefreshLayout mRefreshLayout ;
        private Handler mHandler = new Handler(Looper.getMainLooper());

        private ArrayList<TQCommentOverInfoModel.TQCommentInfoModel> selectedData = new ArrayList<>();

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View lView = inflater.inflate(R.layout.fragment_manager_leavemsg_layout, container, false);
            mRecyclerView = lView.findViewById(R.id.recyclerView);

            if(mManagerMsgAdapter == null){
                mManagerMsgAdapter = new TQManagerMsgAdapter(getContext());
                mManagerMsgAdapter.setSelectedChangeListener(mSelectedChangeListener);
            }
            mRecyclerView.setAdapter(mManagerMsgAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            mRecyclerView.setBackgroundColor(Color.parseColor("#eff1f3"));

            mButton = lView.findViewById(R.id.button);
            mButton.setEnabled(false);
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dataData();
                }
            });

            attId = getActivity().getIntent().getIntExtra("AttachId", 0);
            meterialId = getActivity().getIntent().getIntExtra("meterialId", 0);

            mRefreshLayout = lView.findViewById(R.id.refreshLayout);
            mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    page++;
                    getData();
                }
            });


            mRefreshLayout.setEnableLoadMore(false);
            mRefreshLayout.autoRefresh();
            return lView;
        }


        private void getData(){
            TQNetInterface.getAttComm(attId, page, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    mRefreshLayout.finishRefresh();

                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQDiscussMngFragment.this.getContext(), pNetResult.getErrMsg());
                        return;
                    }

                    mRefreshLayout.finishRefresh();
                    if(pNetResult.getData() == null) return;

                    JSONArray lArray = pNetResult.getData().optJSONArray("comment");
                    if(lArray == null) return;

                    ArrayList<TQCommentOverInfoModel.TQCommentInfoModel> lInfoModels = new ArrayList<>();
                    for (int i = 0; i < lArray.length(); i++){
                        try {
                            JSONObject lObject = lArray.getJSONObject(i);

                            TQCommentOverInfoModel.TQCommentInfoModel lCommentInfoModel = new TQCommentOverInfoModel.TQCommentInfoModel(lObject);
                            lInfoModels.add(0, lCommentInfoModel);

                        }catch (JSONException pE){
                            pE.printStackTrace();
                        }
                    }

                    mManagerMsgAdapter.appendMore(lInfoModels);
                    if(page == 1){
                        scollToBottom();
                    }
                }
            });
        }

        public void scollToBottom(){
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mRecyclerView.scrollToPosition(mManagerMsgAdapter.getItemCount() - 1);
                }
            }, 1);
        }

        private TQManagerMsgAdapter.ISelectedChangeListener mSelectedChangeListener = new TQManagerMsgAdapter.ISelectedChangeListener() {
            @Override
            public void dataChange(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel, boolean selected) {
                if(selected){
                    selectedData.add(pInfoModel);
                }else {
                    selectedData.remove(pInfoModel);
                }

                mButton.setEnabled(selectedData.size() > 0);
            }

            @Override
            public boolean isSelected(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel) {
                return selectedData.contains(pInfoModel);
            }
        };
        private void cleanSelect(){
            selectedData.clear();
            mButton.setEnabled(false);
        }

        private void dataData(){
            if(selectedData.size() <= 0) return;

            ArrayList<Integer> ids = new ArrayList<>();
            for (TQCommentOverInfoModel.TQCommentInfoModel ss: selectedData
                 ) {
                ids.add(ss.getId());
            }

            TQLoadingHelper.build(TQDiscussMngFragment.this.getContext()).setCancelable(false).show();
            TQNetInterface.delComm(ids, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    mManagerMsgAdapter.removeSelect(selectedData);
                    cleanSelect();
                    TQLoadingHelper.dimiss();
                }
            });
        }
    }
}
